{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 1、使用@tool装饰器定义工具\n",
    "\n",
    "举例1："
   ],
   "id": "8afe4e01b00493f0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T01:46:47.220886Z",
     "start_time": "2025-09-30T01:46:46.340537Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from docutils.nodes import description\n",
    "from langchain_core.tools import tool, StructuredTool\n",
    "from pydantic import BaseModel, Field\n",
    "\n",
    "\n",
    "@tool\n",
    "def add_number(a: int, b: int) -> int:\n",
    "    \"\"\"计算两个整数的和\"\"\"\n",
    "    return a + b\n",
    "\n",
    "\n",
    "print(f\"name = {add_number.name}\") #默认是函数的名称\n",
    "print(f\"args = {add_number.args}\")\n",
    "print(f\"description = {add_number.description}\")  #默认是函数的说明信息\n",
    "print(f\"return_direct = {add_number.return_direct}\")  #默认值是False"
   ],
   "id": "dc773660216c2dd9",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/miniconda3/envs/pyth310/lib/python3.10/site-packages/requests/__init__.py:86: RequestsDependencyWarning: Unable to find acceptable character detection dependency (chardet or charset_normalizer).\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = add_number\n",
      "args = {'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}\n",
      "description = 计算两个整数的和\n",
      "return_direct = False\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "举例2：",
   "id": "267529550a3046b6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T01:51:02.765022Z",
     "start_time": "2025-09-30T01:51:02.737417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "@tool(name_or_callable = \"add_two_number\",description=\"add two numbers\",return_direct=True)\n",
    "def add_number(a: int, b: int) -> int:\n",
    "    \"\"\"计算两个整数的和\"\"\"\n",
    "    return a + b\n",
    "\n",
    "\n",
    "print(f\"name = {add_number.name}\")  #add_two_number\n",
    "print(f\"args = {add_number.args}\")\n",
    "print(f\"description = {add_number.description}\")  #add two numbers\n",
    "print(f\"return_direct = {add_number.return_direct}\")  #True"
   ],
   "id": "c20c93f873c9bd89",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = add_two_number\n",
      "args = {'a': {'title': 'A', 'type': 'integer'}, 'b': {'title': 'B', 'type': 'integer'}}\n",
      "description = add two numbers\n",
      "return_direct = True\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T01:54:40.644217Z",
     "start_time": "2025-09-30T01:54:40.570857Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#调用工具\n",
    "add_number.invoke({\"a\": 10, \"b\": 20})"
   ],
   "id": "6f3387340425bfb1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "举例3：修改args参数的描述",
   "id": "156b5f9b24064f44"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T01:58:52.725066Z",
     "start_time": "2025-09-30T01:58:52.715694Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from pydantic import BaseModel, Field\n",
    "class FieldInfo(BaseModel):\n",
    "    a: int = Field(description=\"第1个整型参数\")\n",
    "    b: int = Field(description=\"第2个整型参数\")\n",
    "\n",
    "@tool(name_or_callable=\"add_two_number\", description=\"add two numbers\", return_direct=True, args_schema=FieldInfo)\n",
    "def add_number(a: int, b: int) -> int:\n",
    "    \"\"\"计算两个整数的和\"\"\"\n",
    "    return a + b\n",
    "\n",
    "print(f\"name = {add_number.name}\")  #add_two_number\n",
    "print(f\"args = {add_number.args}\")\n",
    "print(f\"description = {add_number.description}\")  #add two numbers\n",
    "print(f\"return_direct = {add_number.return_direct}\")  #True"
   ],
   "id": "e0f380f725fc152f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = add_two_number\n",
      "args = {'a': {'description': '第1个整型参数', 'title': 'A', 'type': 'integer'}, 'b': {'description': '第2个整型参数', 'title': 'B', 'type': 'integer'}}\n",
      "description = add two numbers\n",
      "return_direct = True\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 2、StructuredTool的from_function()的使用\n",
    "\n",
    "举例1："
   ],
   "id": "90cad6bb0ecfe512"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:02:11.189520Z",
     "start_time": "2025-09-30T02:02:11.175718Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 声明一个函数\n",
    "def search_google(query: str) -> str:\n",
    "    return \"最后查询的结果\"\n",
    "\n",
    "# 定义一个工具\n",
    "search01 = StructuredTool.from_function(\n",
    "    func=search_google,\n",
    "    name=\"Search\",\n",
    "    description=\"查询google搜索引擎，并将结果返回\",\n",
    "                                        )\n",
    "\n",
    "print(f\"name = {search01.name}\")\n",
    "print(f\"args = {search01.args}\")\n",
    "print(f\"description = {search01.description}\")\n",
    "print(f\"return_direct = {search01.return_direct}\")"
   ],
   "id": "52d11684cfff32ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = Search\n",
      "args = {'query': {'title': 'Query', 'type': 'string'}}\n",
      "description = 查询google搜索引擎，并将结果返回\n",
      "return_direct = False\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:03:14.945084Z",
     "start_time": "2025-09-30T02:03:14.932467Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "search01.invoke({\"query\":\"中美AI的发展现状\"})"
   ],
   "id": "ebccb511379860ed",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'最后查询的结果'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "举例2：",
   "id": "9090658df0343efb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:04:06.665054Z",
     "start_time": "2025-09-30T02:04:06.657227Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "from langchain_core.tools.structured import StructuredTool\n",
    "from pydantic import BaseModel,Field\n",
    "\n",
    "class FieldInfo(BaseModel):\n",
    "    query: str = Field(description=\"要检索的关键词\")\n",
    "\n",
    "\n",
    "# 声明一个函数\n",
    "def search_google(query: str):\n",
    "    return \"最后查询的结果\"\n",
    "\n",
    "\n",
    "# 定义一个工具\n",
    "search02 = StructuredTool.from_function(\n",
    "    func=search_google,\n",
    "    name=\"Search\",\n",
    "    description=\"查询google搜索引擎，并将结果返回\",\n",
    "    return_direct=True,\n",
    "    args_schema=FieldInfo\n",
    ")\n",
    "\n",
    "print(f\"name = {search02.name}\")\n",
    "print(f\"args = {search02.args}\")\n",
    "print(f\"description = {search02.description}\")\n",
    "print(f\"return_direct = {search02.return_direct}\")"
   ],
   "id": "d3c4c856635123ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = Search\n",
      "args = {'query': {'description': '要检索的关键词', 'title': 'Query', 'type': 'string'}}\n",
      "description = 查询google搜索引擎，并将结果返回\n",
      "return_direct = True\n"
     ]
    }
   ],
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
